package com.mission.shaco.warehouse.models

import android.app.Activity
import android.content.Context
import org.json.JSONObject
import com.mission.shaco.warehouse.R
import org.json.JSONArray
import org.json.JSONException
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable

/**
 * Created by Administrator on 2017/8/28.
 */

public class Roles(roleList:JSONArray,realName:String) : Serializable {

    @Transient var btnList = JSONObject()
    @Transient var roleList:JSONArray = roleList
    var realName = realName

    init {
        setItem("S01", R.id.stockOutSingle )
        setItem("S02", R.id.stockOutMulti )
        setItem("S03", R.id.btnStockChange )
        setItem("S04", R.id.btnStockChangeStock )
        setItem("S05", R.id.btnCheckQty )
        setItem("S06", R.id.btnStockIn )
    }

    public fun clear(){
        this.roleList = JSONArray()
        this.realName=""
    }
    public fun getMenu():JSONObject {
        if(this.roleList.length() ==0){
            return btnList
        }

        var menuJson = JSONObject()
        for (code in this.btnList.keys()){
            for (i  in 0..this.roleList.length() - 1 ){
                if(code.equals( this.roleList.get(i))){
                    menuJson.put(code, this.btnList.getInt(code))
                    break
                }
            }
        }
        return menuJson
    }

    public fun setItem(code: String, ctrlId:Int ){
        btnList.put(code,ctrlId)
    }

    @Throws(IOException::class)
    private fun writeObject(oos: ObjectOutputStream) {
        oos.defaultWriteObject()
        var allJson = JSONArray()
        allJson.put(0,btnList)
        allJson.put(1,roleList)
        oos.writeObject(allJson.toString())
    }

    @Throws(ClassNotFoundException::class, IOException::class, JSONException::class)
    private fun readObject(ois: ObjectInputStream) {
        ois.defaultReadObject()
        var allJson = JSONArray(ois.readObject() as String)
        btnList = allJson.get(0) as JSONObject
        roleList = allJson.get(1) as JSONArray
    }



}
